﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.UI.MobileControls;
using control.Biz;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace TrangSucShop.administrator.AdminUsercontrol
{
    public partial class bills : System.Web.UI.UserControl
    {
        public SortDirection GridViewSortDirection
        {
            get
            {
                if (ViewState["sort"] == null)
                    ViewState["sort"] = SortDirection.Ascending;

                return (SortDirection)ViewState["sort"];
            }
            set { ViewState["sort"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["ID"] == null)
                {
                    List<BizTrangThai> trangthais = new List<BizTrangThai>();
                    trangthais = BizTrangThai.GetAll();
                    BizTrangThai tatca = new BizTrangThai()
                    {
                        ID = 0,
                        TrangThai = "tất cả"
                    };
                    trangthais.Add(tatca);
                    DropDownList2.DataSource = trangthais;
                    DropDownList2.DataTextField = "TrangThai";
                    DropDownList2.DataValueField = "ID";
                    DropDownList2.SelectedValue = "1";
                    DropDownList2.DataBind();
                    GridView1.DataSource = null;
                    List<BizDonHang> donhangs = new List<BizDonHang>();
                    donhangs = BizDonHang.GetAll();

                    List<BizNgay> ngay = donhangs.GroupBy(p => p.ThoiGian.ToShortDateString()).OrderBy(p => p.Key)
                        .Select(emp => new BizNgay{ ID = emp.Key, Value = emp.Key }).ToList();
                    BizNgay ngaytc = new BizNgay { ID = "", Value = "tất cả" };
                    ngay.Add(ngaytc);
                    DropDownList3.DataSource = ngay;
                    DropDownList3.DataTextField = "Value";
                    DropDownList3.DataValueField = "ID";
                    DropDownList3.DataBind();
                   
                   

                    List<BizDonHang> source = new List<BizDonHang>();
                    source = donhangs.Where(p => p.TrangThai == 1).ToList();
                    if (source.Count != 0)
                    {
                        GridView1.DataSource = source ;
                        GridView1.DataBind();
                        Session["DonHang"] = source;
                    }
                    else
                        Label1.Text = "không có dữ liệu !!!";
                }
                else
                {
                    MT1.ActiveViewIndex=1;
                    int ID = Convert.ToInt32(Request.QueryString["ID"]);
                    BizDonHang donhang = BizDonHang.GetByID(ID);
                    TextBox1.Text = donhang.HoTen;
                    TextBox2.Text = donhang.SoDienThoai;
                    TextBox6.Text = donhang.Email;
                    GridView2.DataSource = donhang.Items;
                    GridView2.DataBind();
                    List<BizTrangThai> trangthais = new List<BizTrangThai>();
                    trangthais = BizTrangThai.GetAll();
                    DropDownList1.DataSource = trangthais;
                    DropDownList1.DataTextField = "TrangThai";
                    DropDownList1.DataValueField = "ID";
                    DropDownList1.SelectedValue = donhang.TrangThai.ToString() ;
                    DropDownList1.DataBind();
                }
            }
            
        }
        public string lblThanhTienValue()
        {
            int ID = Convert.ToInt32(Request.QueryString["ID"]);
            BizDonHang donhang = BizDonHang.GetByID(ID);
            return donhang.TongTien.ToString();
        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            getGridviewSource();
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();

        }
        private void getGridviewSource()
        {
            List<BizDonHang> donhangs = new List<BizDonHang>();
            if (Session["DonHang"] == null)
            {
                donhangs = BizDonHang.GetAll();
                Session["DonHang"] = donhangs;
            }
            else
                donhangs = (List<BizDonHang>)Session["DonHang"];


            GridView1.DataSource = donhangs;
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int ID = Convert.ToInt32(e.CommandArgument);
                BizDonHang sp = BizDonHang.GetByID(ID);
                bool resuil = sp.Detele();
                if (resuil == true)
                {
                    Response.Redirect("bills.aspx");
                }
                else
                    Label1.Text = "Không Thể Xóa Dữ Liệu !!!";

            }
            if (e.CommandName == "Edit")
            {
                Response.Redirect("bills.aspx?ID=" + e.CommandArgument.ToString());
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");

                l.Attributes.Add("onclick", "javascript:return " +
                     "confirm('bạn có muốn xóa record " +
                     DataBinder.Eval(e.Row.DataItem, "ID") + "')");

            }
        }

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            List<BizDonHang> myGridResults = (List<BizDonHang>)Session["DonHang"];


            if (myGridResults != null)
            {
                List<BizDonHang> viewstate = new List<BizDonHang>();
                var param = Expression.Parameter(typeof(BizDonHang), e.SortExpression);
                var sortExpression = Expression.Lambda<Func<BizDonHang, object>>(Expression.Convert(Expression.Property(param, e.SortExpression), typeof(object)), param);


                if (GridViewSortDirection == SortDirection.Ascending)
                {
                    viewstate = myGridResults.AsQueryable<BizDonHang>().OrderBy(sortExpression).ToList<BizDonHang>();
                    GridView1.DataSource = myGridResults.AsQueryable<BizDonHang>().OrderBy(sortExpression).ToList<BizDonHang>();
                    GridViewSortDirection = SortDirection.Descending;
                }
                else
                {
                    viewstate = myGridResults.AsQueryable<BizDonHang>().OrderByDescending(sortExpression).ToList<BizDonHang>();
                    GridView1.DataSource = myGridResults.AsQueryable<BizDonHang>().OrderByDescending(sortExpression).ToList<BizDonHang>();
                    GridViewSortDirection = SortDirection.Ascending;
                };


                GridView1.DataBind();
                Session["DonHang"] = viewstate;
            }

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }
        protected void Button1_Click(object sender, EventArgs e)
        {


            int ID = Convert.ToInt32(Request.QueryString["ID"]);
            BizDonHang donhang = BizDonHang.GetByID(ID);
            if (donhang != null)
            {
                donhang.TrangThai = Convert.ToInt32(DropDownList1.SelectedValue);
                donhang.Update();
            }
            Response.Redirect("bills.aspx");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(DropDownList2.SelectedValue);
            string tungay = DropDownList3.SelectedValue;
            List<BizDonHang> donhangs = new List<BizDonHang>();
            donhangs = BizDonHang.GetAll();
            List<BizDonHang> source = new List<BizDonHang>();
            if (id != 0)
                source = donhangs.Where(p => (p.TrangThai == id&&p.ThoiGian.ToShortDateString().Contains(tungay))).ToList();
            else
                source = donhangs;
            GridView1.DataSource = null;
            if (source.Count != 0)
            {

                GridView1.DataSource = source;

                Session["DonHang"] = source;
            }
            else
                Label1.Text = "không có dữ liệu !!!";
            GridView1.DataBind();
        }
    }
}